package bpui.utils;

/*
 * Copyright (C) 2010 Andrea Ferrandi <aferrandi@users.sourceforge.net>. 
 * Released under the terms of the GNU GPL v3.0.
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/**
 * @author SHZ Aug 1, 2010
 * This Test class is based on the SWT snippet http://dev.eclipse.org/viewcvs/index.cgi/~checkout~/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet38.java
 * protected by the Eclipse Public License v1.0 (http://www.eclipse.org/legal/epl-v10.html)
 */
public class TestTableRowHeader
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		Display display = new Display ();
		Shell shell = new Shell (display);
		GridLayout layout = new GridLayout(2, false);
		shell.setLayout(layout);
		// to avoid spaces between table and row header
		layout.horizontalSpacing = 0;

		// the text of the row header button is the row index itself
		TableRowHeader header = new TableRowHeader(shell, new TableRowHeader.IRowHeaderTextListener()
		{		
			public String textForRow(int row)
			{
				return String.valueOf(row);
			}
		});
		
		GridData data2 = new GridData(SWT.FILL, SWT.FILL, false, true);
		header.setLayoutData(data2);
		
		
		final Table table = new Table (shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
		table.setLinesVisible (true);
		table.setHeaderVisible (true);
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		data.heightHint = 200;
		table.setLayoutData(data);			
		String[] titles = {" ", "C", "!", "Description", "Resource", "In Folder", "Location"};
		for (int i=0; i<titles.length; i++) {
			TableColumn column = new TableColumn (table, SWT.NONE);
			column.setText (titles [i]);
		}	
		
		header.init(table);
		// when the user presses one of the row header button, the related row in the table
		// is selected
		header.addRowHeaderSelectionListener(new TableRowHeader.IRowHeaderSelectionListener()
		{			
			public void widgetSelected(SelectionEvent ev, int rowIndex)
			{
				table.deselectAll();
				table.select(rowIndex);
			}
		});
		
		int count = 128;
		for (int i=0; i<count; i++) {
			TableItem item = new TableItem (table, SWT.NONE);
			item.setText (0, "x");
			item.setText (1, "y");
			item.setText (2, "!");
			item.setText (3, "this stuff behaves the way I expect");
			item.setText (4, "almost everywhere");
			item.setText (5, "some.folder");
			item.setText (6, "line " + i + " in nowhere");
		}
		for (int i=0; i<titles.length; i++) {
			table.getColumn (i).pack ();
		}	
		shell.pack ();
		shell.open ();
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		display.dispose ();
		
	}

}